<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<head>

<meta charset="utf-8">
<title>关于 Mac 自带的Apache如何开启</title>


<style type="text/css">
body {
  font-family: Helvetica, arial, sans-serif;
  font-size: 14px;
  line-height: 1.6;
  padding-top: 10px;
  padding-bottom: 10px;
  background-color: white;
  padding: 30px; }

body > *:first-child {
  margin-top: 0 !important; }
body > *:last-child {
  margin-bottom: 0 !important; }

a {
  color: #4183C4; }
a.absent {
  color: #cc0000; }
a.anchor {
  display: block;
  padding-left: 30px;
  margin-left: -30px;
  cursor: pointer;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0; }

h1, h2, h3, h4, h5, h6 {
  margin: 20px 0 10px;
  padding: 0;
  font-weight: bold;
  -webkit-font-smoothing: antialiased;
  cursor: text;
  position: relative; }

h1:hover a.anchor, h2:hover a.anchor, h3:hover a.anchor, h4:hover a.anchor, h5:hover a.anchor, h6:hover a.anchor {
  background: url() no-repeat 10px center;
  text-decoration: none; }

h1 tt, h1 code {
  font-size: inherit; }

h2 tt, h2 code {
  font-size: inherit; }

h3 tt, h3 code {
  font-size: inherit; }

h4 tt, h4 code {
  font-size: inherit; }

h5 tt, h5 code {
  font-size: inherit; }

h6 tt, h6 code {
  font-size: inherit; }

h1 {
  font-size: 28px;
  color: black; }

h2 {
  font-size: 24px;
  border-bottom: 1px solid #cccccc;
  color: black; }

h3 {
  font-size: 18px; }

h4 {
  font-size: 16px; }

h5 {
  font-size: 14px; }

h6 {
  color: #777777;
  font-size: 14px; }

p, blockquote, ul, ol, dl, li, table, pre {
  margin: 15px 0; }

hr {
  background: transparent url() repeat-x 0 0;
  border: 0 none;
  color: #cccccc;
  height: 4px;
  padding: 0;
}

body > h2:first-child {
  margin-top: 0;
  padding-top: 0; }
body > h1:first-child {
  margin-top: 0;
  padding-top: 0; }
  body > h1:first-child + h2 {
    margin-top: 0;
    padding-top: 0; }
body > h3:first-child, body > h4:first-child, body > h5:first-child, body > h6:first-child {
  margin-top: 0;
  padding-top: 0; }

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
  margin-top: 0;
  padding-top: 0; }

h1 p, h2 p, h3 p, h4 p, h5 p, h6 p {
  margin-top: 0; }

li p.first {
  display: inline-block; }
li {
  margin: 0; }
ul, ol {
  padding-left: 30px; }

ul :first-child, ol :first-child {
  margin-top: 0; }

dl {
  padding: 0; }
  dl dt {
    font-size: 14px;
    font-weight: bold;
    font-style: italic;
    padding: 0;
    margin: 15px 0 5px; }
    dl dt:first-child {
      padding: 0; }
    dl dt > :first-child {
      margin-top: 0; }
    dl dt > :last-child {
      margin-bottom: 0; }
  dl dd {
    margin: 0 0 15px;
    padding: 0 15px; }
    dl dd > :first-child {
      margin-top: 0; }
    dl dd > :last-child {
      margin-bottom: 0; }

blockquote {
  border-left: 4px solid #dddddd;
  padding: 0 15px;
  color: #777777; }
  blockquote > :first-child {
    margin-top: 0; }
  blockquote > :last-child {
    margin-bottom: 0; }

table {
  padding: 0;border-collapse: collapse; }
  table tr {
    border-top: 1px solid #cccccc;
    background-color: white;
    margin: 0;
    padding: 0; }
    table tr:nth-child(2n) {
      background-color: #f8f8f8; }
    table tr th {
      font-weight: bold;
      border: 1px solid #cccccc;
      margin: 0;
      padding: 6px 13px; }
    table tr td {
      border: 1px solid #cccccc;
      margin: 0;
      padding: 6px 13px; }
    table tr th :first-child, table tr td :first-child {
      margin-top: 0; }
    table tr th :last-child, table tr td :last-child {
      margin-bottom: 0; }

img {
  max-width: 100%; }

span.frame {
  display: block;
  overflow: hidden; }
  span.frame > span {
    border: 1px solid #dddddd;
    display: block;
    float: left;
    overflow: hidden;
    margin: 13px 0 0;
    padding: 7px;
    width: auto; }
  span.frame span img {
    display: block;
    float: left; }
  span.frame span span {
    clear: both;
    color: #333333;
    display: block;
    padding: 5px 0 0; }
span.align-center {
  display: block;
  overflow: hidden;
  clear: both; }
  span.align-center > span {
    display: block;
    overflow: hidden;
    margin: 13px auto 0;
    text-align: center; }
  span.align-center span img {
    margin: 0 auto;
    text-align: center; }
span.align-right {
  display: block;
  overflow: hidden;
  clear: both; }
  span.align-right > span {
    display: block;
    overflow: hidden;
    margin: 13px 0 0;
    text-align: right; }
  span.align-right span img {
    margin: 0;
    text-align: right; }
span.float-left {
  display: block;
  margin-right: 13px;
  overflow: hidden;
  float: left; }
  span.float-left span {
    margin: 13px 0 0; }
span.float-right {
  display: block;
  margin-left: 13px;
  overflow: hidden;
  float: right; }
  span.float-right > span {
    display: block;
    overflow: hidden;
    margin: 13px auto 0;
    text-align: right; }

code, tt {
  margin: 0 2px;
  padding: 0 5px;
  white-space: nowrap;
  border: 1px solid #eaeaea;
  background-color: #f8f8f8;
  border-radius: 3px; }

pre code {
  margin: 0;
  padding: 0;
  white-space: pre;
  border: none;
  background: transparent; }

.highlight pre {
  background-color: #f8f8f8;
  border: 1px solid #cccccc;
  font-size: 13px;
  line-height: 19px;
  overflow: auto;
  padding: 6px 10px;
  border-radius: 3px; }

pre {
  background-color: #f8f8f8;
  border: 1px solid #cccccc;
  font-size: 13px;
  line-height: 19px;
  overflow: auto;
  padding: 6px 10px;
  border-radius: 3px; }
  pre code, pre tt {
    background-color: transparent;
    border: none; }

sup {
    font-size: 0.83em;
    vertical-align: super;
    line-height: 0;
}
* {
	-webkit-print-color-adjust: exact;
}
@media screen and (min-width: 914px) {
    body {
        width: 854px;
        margin:0 auto;
    }
}
@media print {
	table, pre {
		page-break-inside: avoid;
	}
	pre {
		word-wrap: break-word;
	}
}
</style>


</head>

<body>

<h1 id="toc_0">关于 Mac 自带的Apache如何开启</h1>

<h2 id="toc_1"></h2>

<p>首先我要说的就是，其实 &nbsp;&nbsp;&nbsp; <strong>Mac自带的 Apache 是默认开启的!!</strong></p>

<h3 id="toc_2">本文主要讲述 mac 如何开启自带的Apache 服务器以及过程中出现的一个小问题</h3>

<p>看本文您需要会基本的 shell 知识</p>

<p>我电脑的基本信息：</p>

<ul>
<li>电脑购买时期：MacBook Pro (Retina, 13-inch, Early 2015)（128G的乞丐版）</li>
<li>系统版本：10.12.3   MacOS Sierra</li>
</ul>

<p>在网上看了好多的教程，都是大同小异，并没有从根本上解决我的问题</p>

<p>在此自己写个文档</p>

<h2 id="toc_3">第一步，启动服务器</h2>

<ul>
<li>查看电脑Apache的版本和php的版本</li>
</ul>

<div><pre class="line-numbers"><code class="language-javascript">sudo apachectl -v
php -v</code></pre></div>

<p><img src="./img/Apache1.png" alt="image"></p>

<p>全都是以下这种方式</p>

<div><pre class="line-numbers"><code class="language-javascript">sudo apachectl start</code></pre></div>

<p>然后就说什么Apache服务器就启动了什么的</p>

<ul>
<li>运行上面的代码之后如下图所示，显示的这个东西</li>
</ul>

<p><img src="./img/Apache2.png" alt="image"></p>

<p>打印这个东西</p>

<div><pre class="line-numbers"><code class="language-javascript">
/System/Library/LaunchDaemons/org.apache.httpd.plist: service already loaded

</code></pre></div>

<p>意思就是 服务器已经加载</p>

<p>但是当我在浏览器输入 http://127.0.0.1  或者 http://localhost  的时候，发现总是提示“无法连接到服务器”。在网上找了很多的方法，全都不起作用，最后用此办法解决了该问题。</p>

<p>解决方法：</p>

<ol>
<li><p>在log目录下，新建一个名为apache2的文件夹（终端：$ sudo mkdir /private/var/log/apache2）</p></li>
<li><p>然后重启apache（终端：$ sudo apachectl restart） </p></li>
</ol>

<p><img src="./img/Apache3.png" alt="image"></p>

<p>这样会自动在apache2里面重新生成apache需要的日志，便可正常访问和使用apache服务了。</p>

<h2 id="toc_4">第二步，更改默认的文件路径位置</h2>

<p>默认的位置是    /Library/WebServer/Documents</p>

<p>比如我想将其改到桌面上</p>

<ul>
<li>点击“前往”，点击“前往文件夹”，然后输入以下路径并回车</li>
</ul>

<p><img src="./img/Apache4.png" alt="image"></p>

<ul>
<li>在其中找到 Apache2 的文件夹，打开之后找到以下文件</li>
</ul>

<p><img src="./img/Apache5.png" alt="image"></p>

<p>由于mac的权限问题，所以需要将这个文件拖到桌面上，打开进行编辑</p>

<p>将此处的路径改成自己想要的路径</p>

<p><img src="./img/Apache6.png" alt="image"></p>

<p>然后重启服务器</p>

<div><pre class="line-numbers"><code class="language-javascript">sudo apachectl restart</code></pre></div>

<p>然后打开 127.0.0.1 就可以看到自己的目录里面的东西了</p>

<h2 id="toc_5">第三步，启动自带的php（否则不能运行php文件）</h2>

<p>仍然是打开刚刚的那个文件</p>

<p>搜索以下代码，并去掉前面  # 号</p>

<div><pre class="line-numbers"><code class="language-javascript">#LoadModule php5_module libexec/apache2/libphp5.so</code></pre></div>

<p>保存并退出</p>

<p>重启服务器</p>

<div><pre class="line-numbers"><code class="language-javascript">sudo apachectl restart</code></pre></div>

<p>打开127.0.0.1就可以看到写的php文件了</p>

<p>其他的关于更改域名，host文件之类的，网上很多的教程，在此就不赘述了</p>




</body>

</html>
